<template>
  <el-form label-width="100px">
    <el-form-item label="SKU名称">
      <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input placeholder="价格(元)" v-model="skuParams.price"></el-input>
    </el-form-item>
    <el-form-item label="重量(克)">
      <el-input placeholder="重量(克)" v-model="skuParams.weight"></el-input>
    </el-form-item>
    <el-form-item label="SKU描述">
      <el-input
        type="textarea"
        placeholder="SKU描述"
        v-model="skuParams.skuDesc"
      ></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form :inline="true" label-width="100px">
        <el-form-item
          v-for="item in attr"
          :key="item.id"
          :label="item.attrName"
          style="margin: 5px 0px"
        >
          <el-select v-model="item.idAndValueId" style="width: 150px">
            <el-option
              v-for="attrValue in item.attrValueList"
              :key="attrValue.id"
              :label="attrValue.valueName"
              :value="`${item.id}:${attrValue.id}`"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form :inline="true" label-width="100px">
        <el-form-item
          v-for="item in saleAttr"
          :key="item.id"
          :label="item.saleAttrName"
          style="margin: 5px 0px"
        >
          <el-select v-model="item.saleIdAndValueId" style="width: 150px">
            <el-option
              v-for="saleAttrValue in item.spuSaleAttrValueList"
              :key="saleAttrValue.id"
              :label="saleAttrValue.saleAttrValueName"
              :value="`${item.id}:${saleAttrValue.id}`"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="imageList" ref="table">
        <el-table-column type="selection"></el-table-column>
        <el-table-column label="图片">
          <template #="{ row }">
            <el-image :src="row.imgUrl" alt="图片无法显示"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <template #="{ row }">
            <el-button type="warning" @click="handler(row)">设置默认</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <template #default>
        <el-button @click="save" type="primary">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </template>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
  import { reqAttrInfoList } from '@/api/product/attr'
  import {
    reqSpuImageList,
    reqSpuSaleAttr,
    reqSaveSkuInfo,
  } from '@/api/product/spu'
  import { reactive, ref } from 'vue'
  import type { SkuData } from '@/api/product/spu/type'
  import { ElMessage } from 'element-plus'

  let $emit = defineEmits(['changeScene'])
  const cancel = () => {
    $emit('changeScene', { flag: 0, toWhichPage: '' })
  }

  let attr = ref<any>([])
  let saleAttr = ref<any>([])
  let imageList = ref<any>([])
  let skuParams = reactive<SkuData>({
    category3Id: '',
    spuId: '',
    tmId: '',
    skuName: '',
    price: '',
    weight: '',
    skuDesc: '',
    skuAttrValueList: [],
    skuSaleAttrValueList: [],
    skuDefaultImg: '',
  })

  let table = ref() //表格组件实例

  //初始化获取数据的方法
  const initSkuData = async (
    c1Id: number | string,
    c2Id: number | string,
    c3Id: number | string,
    row: any,
  ) => {
    //清空已有数据
    Object.assign(skuParams, {
      category3Id: '',
      spuId: '',
      tmId: '',
      skuName: '',
      price: '',
      weight: '',
      skuDesc: '',
      skuAttrValueList: [],
      skuSaleAttrValueList: [],
      skuDefaultImg: '',
    })
    //收集数据
    skuParams.category3Id = c3Id
    skuParams.spuId = row.id
    skuParams.tmId = row.tmId
    //1发请求
    //1.1请求平台属性数据
    //1.2请求该spu下销售属性数据
    //1.3请求该spu下图片列表数据
    let result = await reqAttrInfoList(c1Id, c2Id, c3Id)
    let result1 = await reqSpuSaleAttr(row.id)
    let result2 = await reqSpuImageList(row.id)
    //2存数据
    attr.value = result.data
    saleAttr.value = result1.data
    imageList.value = result2.data
  }

  //对外暴露方法
  defineExpose({ initSkuData })

  //设置默认按钮点击事件回调
  const handler = (row: any) => {
    imageList.value.forEach((item: any) => {
      table.value.toggleRowSelection(item, false)
    })
    table.value.toggleRowSelection(row, true)

    skuParams.skuDefaultImg = row.imgUrl
  }

  const save = async () => {
    //整理参数
    skuParams.skuAttrValueList = attr.value.reduce((prev: any, next: any) => {
      if (next.idAndValueId) {
        let [attrId, valueId] = next.idAndValueId.split(':')
        prev.push({ attrId, valueId })
      }
      return prev
    }, [])
    skuParams.skuSaleAttrValueList = saleAttr.value.reduce(
      (prev: any, next: any) => {
        if (next.saleIdAndValueId) {
          let [saleAttrid, saleAttrValueId] = next.saleIdAndValueId.split(':')
          prev.push({ saleAttrid, saleAttrValueId })
        }
        return prev
      },
      [],
    )
    //发请求
    let result = await reqSaveSkuInfo(skuParams)
    //根据结果进行提示
    if (result.code === 200) {
      ElMessage({
        type: 'success',
        message: '添加SKU成功',
      })
      $emit('changeScene', { flag: 0, toWhichPage: '' })
    } else {
      ElMessage({
        type: 'error',
        message: '添加SKU失败',
      })
    }
  }
</script>

<style scoped></style>
